﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSNgachBacLuong : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
      
        public string TenBacLuong { get; set; }
        public string TenHeSo { get; set; }
        public void EditNBL(int hdId, string ngachCongChuc, string mangach, int bacLuongId, decimal heSoId, DateTime ngayHieuLuc, string ghiChu,string lyDo)
        {
            List<NSNgachBacLuong> lst = (List<NSNgachBacLuong>)HttpContext.Current.Session["NBL"];
            NSNgachBacLuong pc = lst.Where(a => a.NgachId.Equals(hdId)).SingleOrDefault();
            if (!ngayHieuLuc.Year.Equals(1))
            {
                pc.NgayHieuLuc = ngayHieuLuc;
            }
            pc.LyDo = lyDo;
            pc.GhiChu = ghiChu;
            pc.TenNgach = ngachCongChuc;
            pc.MaNgach = mangach;
            pc.BacLuongID = bacLuongId;
            pc.HeSo = heSoId;           
            pc.TenBacLuong = common.LoadCommonCodeById(bacLuongId);
            
        }
        public void DeleteNBL(int hdId)
        {
            List<NSNgachBacLuong> lst = (List<NSNgachBacLuong>)HttpContext.Current.Session["NBL"];
            NSNgachBacLuong pc = lst.Where(a => a.NgachId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllNBLByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSNgachBacLuongs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSNgachBacLuongs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateNBL_NhanVien(int nhanVienId, List<NSNgachBacLuong> nblObject)
        {

            var queryNBLByNhanVienId = (from a in db.NSNgachBacLuongs
                                         where a.NhanVienID.Equals(nhanVienId)
                                         select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSNgachBacLuongs.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSNgachBacLuong pc in nblObject)
            {
                NSNgachBacLuong item = new NSNgachBacLuong();
                CopyNSNgachBacLuong(pc, item);
                db.NSNgachBacLuongs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSNgachBacLuong(NSNgachBacLuong src, NSNgachBacLuong des)
        {
            des.LyDo = src.LyDo;
            des.NgayHieuLuc = src.NgayHieuLuc;
            des.GhiChu = src.GhiChu;
            des.NhanVienID = src.NhanVienID;
            des.TenNgach = src.TenNgach;
            des.MaNgach = src.MaNgach;
            des.HeSo = src.HeSo;
            des.BacLuongID = src.BacLuongID;
            des.TenBacLuong = src.TenBacLuong;
            des.TenHeSo = src.TenHeSo;
         

        }
        public List<NSNgachBacLuong> LoadNBLByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSNgachBacLuongs
                         where a.NhanVienID.Equals(nhanVienId)
                         orderby a.NgayHieuLuc descending
                         select a).ToList();
            foreach (NSNgachBacLuong item in query)
            {
            
                item.TenBacLuong = common.LoadCommonCodeById((int)item.BacLuongID);
              
            }
            return query;

        }
        public NSNgachBacLuong GetNgachBacLuong(int nhanVienId, string ngachCongChuc, string mangach, int bacLuongId, decimal heSoId, DateTime ngayHieuLuc, string ghiChu,string lyDo)
        {
            var db = new DataClassesDataContext();
            NSNgachBacLuong hd = new NSNgachBacLuong();
            hd.NgachId = 0 - DateTime.Now.Second;
            if (!ngayHieuLuc.Year.Equals(1))
            {
                hd.NgayHieuLuc = ngayHieuLuc;
            }
            hd.LyDo = lyDo;
            hd.GhiChu = ghiChu;          
            hd.NhanVienID = nhanVienId;
            hd.TenNgach = ngachCongChuc;
            hd.MaNgach = mangach;
            hd.BacLuongID = bacLuongId;
            hd.HeSo = heSoId;
       
            hd.TenBacLuong = common.LoadCommonCodeById(bacLuongId);
         
            return hd;
        }
    }

}